package com.gilight.base.realm;

import com.gilight.base.base.LoginUserInfo;
import com.gilight.base.dao.SysPermissionMapper;
import com.gilight.base.dao.SysRoleMapper;
import com.gilight.base.dao.SysUserMapper;
import com.gilight.base.model.SysPermission;
import com.gilight.base.model.SysRole;
import com.gilight.base.model.SysUser;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class SysUserRealm extends AuthorizingRealm {

    private static Logger logger = LoggerFactory.getLogger(SysUserRealm.class);

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        SysUser sysUser = (SysUser) principalCollection.getPrimaryPrincipal();
        try {
            //注入角色(查询所有的角色注入控制器）
            List<SysRole> list = sysRoleMapper.selectByUserId(sysUser.getId());
            for (SysRole role: list){
                authorizationInfo.addRole(role.getId().toString());
            }
            //注入角色所有权限（查询用户所有的权限注入控制器）
            List<SysPermission>  permissions= sysPermissionMapper.selectByUserId(sysUser.getId());
            for(SysPermission sysPermission:permissions){
                authorizationInfo.addStringPermission(sysPermission.getId().toString());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String userCode = (String) authenticationToken.getPrincipal();
        SysUser sysUser = sysUserMapper.selectByUserCode(userCode);
        LoginUserInfo loginUser = new LoginUserInfo();
        if (sysUser == null) {
            throw new AuthenticationException("用户不存在");
        }
        // 赋值
        BeanUtils.copyProperties(sysUser, loginUser);

        ByteSource credentialsSalt = ByteSource.Util.bytes(sysUser.getSalt()==null?"":sysUser.getSalt());
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(loginUser, sysUser.getPassword(),credentialsSalt ,this.getName());
        return authenticationInfo;
    }
}
